package data_report

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 首提报表
func NewWithdrawReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawReportLogic {
	return &WithdrawReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WithdrawReportLogic) WithdrawReport(req *types.WithdrawReportRequest) (resp types.WithdrawReportResponse, err error) {
	resp = types.WithdrawReportResponse{
		Data:     make([]types.WithdrawReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.SearchArUserWithdrawFirstAnalysisReq{
		Page:          req.Page,
		Limit:         req.PageSize,
		CreatedAt:     req.StartTime,
		UserId:        req.UserId,
		OrderNo:       req.OrderNo,
		FinishAt:      req.EndTime,
		Source:        req.ChannelId,
		WithdrawCount: req.Type,
	}

	analysis, err := l.svcCtx.Analysis.SearchArUserWithdrawFirstAnalysis(l.ctx, reqq)
	if err != nil {
		return resp, err
	}

	for _, v := range analysis.ArUserWithdrawFirstAnalysis {
		t := types.WithdrawReportData{
			Date:         strconv.FormatInt(v.CreatedAt, 10),
			UserId:       v.UserId,
			ParentId1:    v.ParentId,
			ParentId2:    v.UpupParentId,
			ParentId3:    v.UpupupParentId,
			Account:      v.UserAccount,
			OrderNo:      v.OrderNo,
			Amount:       v.WithdrawAmount,
			CompleteTime: v.FinishAt,
		}
		resp.Data = append(resp.Data, t)
	}
	resp.Total = analysis.Total
	return
}
